package com.yoda.mall.admin.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.FlashPromotionProductRelationApi;
import com.yoda.mall.api.ProductApi;
import com.yoda.mall.model.entity.sms.Coupon;
import com.yoda.mall.model.entity.sms.FlashPromotionProductRelation;
import com.yoda.mall.model.vo.FlashPromotionProductRelationVo;
import com.yoda.mall.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class FlashProductRelationService {
    @DubboReference
    private FlashPromotionProductRelationApi flashPromotionProductRelationApi;
    @DubboReference
    private ProductApi productApi;

    public void createFlashProductRelation(List<FlashPromotionProductRelation> flashPromotionProductRelations) {
        flashPromotionProductRelations.forEach(flashPromotionProductRelation -> flashPromotionProductRelationApi.addFlashPromotionProductRelation(flashPromotionProductRelation));
    }

    public void deleteFlashProductRelation(Long id) {
        flashPromotionProductRelationApi.deleteFlashProductRelationById(id);
    }

    public PageResult<FlashPromotionProductRelationVo> flashProductRelationListPage(Long flashPromotionId, Long flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        Page<FlashPromotionProductRelation> page = flashPromotionProductRelationApi.getFlashProductRelationListPage(flashPromotionId, flashPromotionSessionId, pageNum, pageSize);
        List<FlashPromotionProductRelationVo> collect = page.getRecords().stream().map(flashPromotionProductRelation -> {
            FlashPromotionProductRelationVo flashPromotionProductRelationVo = new FlashPromotionProductRelationVo();
            BeanUtils.copyProperties(flashPromotionProductRelation, flashPromotionProductRelationVo);
            flashPromotionProductRelationVo.setProduct(productApi.getProductById(flashPromotionProductRelation.getProductId()));
            return flashPromotionProductRelationVo;
        }).collect(Collectors.toList());
        return new PageResult<>((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), collect);
    }

    public void updateFlashPromotionProductRelation(FlashPromotionProductRelation flashPromotionProductRelation, Long id) {
        flashPromotionProductRelation.setId(id);
        flashPromotionProductRelationApi.updateFlashPromotionProductRelation(flashPromotionProductRelation);
    }

    public FlashPromotionProductRelation getFlashPromotionProductRelation(Long id) {
        return flashPromotionProductRelationApi.getFlashPromotionProductRelationById(id);
    }
}
